package com.hxkj.controller.article;

import com.hxkj.client.service.MessageClient;
import com.hxkj.domain.po.CommonMessage;
import com.hxkj.entity.enums.SmsExceptionEnum;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.result.ResultMessageStruct;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * created by chenfangzhou on 2018/7/13
 */
@RestController
@RequestMapping("/message")
public class SendSmsController {

    private static final Logger logger = LoggerFactory.getLogger(SendSmsController.class);
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MessageClient messageClient;

    /**
     * 发送短信验证码
     */
    @RequestMapping(value = "/sendsms")
    public ResultMessageStruct sendSms(HttpServletRequest request, String mobile, String code) throws Exception {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();

        //首先验证图片验证码是否正确
        String uuid = "";
        Cookie[] cookies = request.getCookies();
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if ("COOKIE_NAME_UUID".equals(cookie.getName())) {
                    uuid = cookie.getValue();
                }
            }
            if (StringUtils.isBlank(mobile)) {
                throw new CustomException(SmsExceptionEnum.getEnumByCode(600));
            }
            if (StringUtils.isBlank(code)) {
                throw new CustomException(SmsExceptionEnum.getEnumByCode(601));
            }
            if (StringUtils.isBlank(uuid)) {
                throw new CustomException(SmsExceptionEnum.getEnumByCode(602));
            }
            if(redisTemplate.boundValueOps(uuid).get() != null) {
                String remoteCode = redisTemplate.boundValueOps(uuid).get().toString();
                if (!code.equals(remoteCode)) {
                    throw new CustomException(SmsExceptionEnum.getEnumByCode(603));
                }
            }
            redisTemplate.delete(uuid);
            //生成随机四位码
            String msg = Integer.toString((int) ((Math.random() * 9 + 1) * 1000));
            logger.info("生成的随机短信验证码:" + msg);
            //存入Redis
            redisTemplate.boundValueOps(mobile).set(msg);
            CommonMessage message = CommonMessage.builder().messageType("登录").message(msg).telephone(mobile).serviceId("cms-management-router").build();
            resultMessageStruct = messageClient.sendMessage(message);
            logger.info(resultMessageStruct.toString());
            return resultMessageStruct;
        }
        resultMessageStruct.setCode(200);
        resultMessageStruct.setMessage("发送成功!");
        return resultMessageStruct;
    }

}

